/**
 * Copyright (c) 2015 XiaoYuanShenQi. All rights reserved.

 * Author: newma
 * Date: 6/29/15
 * Descript: 字符串帮助类

 *  Modified:
 */

var Helper = {};

/**
 *  检测字符串是否全为数字
 *  @param str 要检测的字符串
 *  @return {boolean}  true＝全为数字；false－不全为数字。
 */
Helper.checkNum = function(str) {
    if (str != null && str.length > 0) {
        var reg = /\d*/i;
        var ret = str.match(reg);
        return (ret == str) ? true : false;
    }
    return false;
};

/**
 * 检测指定字符串是否包含有 ^.<>%&',;=?$"':#@!~][{}\/`| 等等特殊字符
 * @param str
 * @returns {boolean} true: 代表包含有非法字符；false：代表无包含非法字符
 */
Helper.checkInvalidateChar = function(str) {

    if (str != null && str.length > 0) {
        var reg = /^(([^\^\.<>%&',;=?$"':#@!~\]\[{}\\/`\|])*)$/;
        var ret = str.match(reg);

        return (ret == null) ? true : false;
    }

    return false;
};


/**
 * 自动生成Sql查询语句
 * @type {number}       查询的类型，取值如下的Helper.SQL_XXXXX
 * @table {string}      要查询的表名
 * @obj {object}        不同type类型含义不同：insert/update时，为json对象，各项属性名应同数据库的表项名一致（注意，各属性的取值不会对结果造成影响）；select/delete时，此处为option（option内属性名为查询条件，当其值为true时才有效。例如
 *                                  {  id: true, name: false } 结果生成的查询条件仅id一项）
 * @options {object}    当insert/update时才有效：insert时，options中的属性不插入数据库中的属性
 *                                             update时，options中的属性为查询条件。
 */
Helper.SQL_INSERT = 0;
Helper.SQL_UPDATE = 1;
Helper.SQL_DELETE = 2;
Helper.SQL_SELECT = 3;
Helper.autoSimpleSQLQueryString = function(type, table, obj, options) {
    var retString = '';

    switch (type) {
        case Helper.SQL_INSERT:
            retString = 'INSERT INTO `xysq`.`' + table + '` (';

            var start = 0;
            var aryCond = [];
            var aryVal = [];

            for (var prop in obj) {
                if (options && options.hasOwnProperty(prop) && options[prop]) {
                    continue;
                }

                aryCond[start] = '`'+ prop + '`';
                aryVal[start] = ':' + prop;

                start++;
            }

            retString += aryCond.join(', ');
            retString += ') VALUES (' + aryVal.join(', ') + ')';
            break;
        case Helper.SQL_UPDATE:
            retString = 'UPDATE `xysq`.`' + table + '` SET ';

            var aryExp = [];
            var start = 0;
            for (var prop in obj) {
                aryExp[start++] = '`' + prop + '` = :' + prop;
            }

            retString += aryExp.join(', ') + ' WHERE (';

            var aryCond = [];
            start = 0;
            for (var prop2 in options) {
                aryCond[start++] = '`' + prop2 + '` = :' + prop2;
            }

            retString += aryCond.join(' AND ') + ')';
            break;
        case Helper.SQL_DELETE:
            retString = 'DELETE FROM `xysq`.`' + table + '`';
            var aryCond = [];
            var start = 0;
            if (obj) {
                for (var prop in obj) {
                    aryCond[start++] = '`' + prop + '` = :' + prop;
                }
            }

            if (start > 0) {
                retString += ' WHERE (' + aryCond.join(' AND ') + ')';
            }

            break;
        case Helper.SQL_SELECT:
            retString = 'SELECT * FROM `xysq`.`' + table + '`';

            var aryCond = [];
            var start = 0;
            if (obj) {
                for (var prop in obj) {
                    aryCond[start++] = '`' + prop + '` = :' + prop;
                }
            }

            if (start > 0) {
                retString += ' WHERE (' + aryCond.join(' AND ') + ')';
            }
            break;
    }

    retString += ';';
    return retString;
}

/**
 * 验证手机号码的有效性
 * @param mobile  手机号码
 * @param cb      function(err)
 * @returns {boolean}  true为有效号码；false为无效号码
 */
Helper.checkPhoneNumValidate = function(mobile, cb) {

    if(!mobile || mobile.length == 0) {
        if (cb != undefined && typeof cb == 'function') cb(new Error('手机号码为空'));
        return false;
    }
    if(mobile.length != 11) {
        if (cb != undefined && typeof cb == 'function') cb(new Error('无效手机号码'));
        return false;
    }
    var myreg = /^(1+\d{10})$/;
    if(!myreg.test(mobile)) {
        if (cb != undefined && typeof cb == 'function') cb(new Error('无效手机号码'));
        return false;
    }

    if (cb != undefined && typeof cb == 'function') {
        cb();
    }
    return true;
};

module.exports = Helper;
